package com.hns.can.safety.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hns.can.master.service.TendencyChartsDataTools;
import com.hns.can.safety.vo.UnsafeBhvTimeSummery;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 安全首页service
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class HomeSafeService extends CommonService {
	@Autowired
	private TendencyChartsDataTools tendencyChartsDataTools;	//趋势图补充数据工具
	
	private List<String> arr = Arrays.asList(new String[]{"vehicleFaultsTendency","unsafeBhvTendency","harmfulTendency"});
	/**
	 * 执行各项子功能db操作
	 * @param funcType 子功能映射名称(根据不同映射名称执行对应db命令)
	 * @param map 参数
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<T> callMethod(String funcType, Map<String, Object> map, String className) throws Exception{
		Class<?> clazz = Class.forName("com.hns.can.safety.vo."+className);
		List list = this.sqlMapDao.selectList("homeSafe."+funcType, map);//
//		try{
//		}catch(Exception e){
//			System.out.println(className+"==========================="+funcType);
//			e.printStackTrace();
//		}
		//TODO 补充趋势图缺失数据 暂时屏蔽
		if(arr.contains(funcType) && PubUtil.isNotEmpty(list)){
			list = tendencyChartsDataTools.fillSafetyMultiTendencyData(list, (String)map.get("dateType"));
		}
		List voList = PubUtil.convertListMapToObject(list, clazz);
		return voList;
	}
	
	
	@SuppressWarnings("rawtypes")
	public ArrayList<UnsafeBhvTimeSummery> getUnsafeTimeData(HashMap map) throws Exception{
		List list = this.sqlMapDao.selectList("homeSafe.getUnsafeTimeData", map);
		ArrayList<UnsafeBhvTimeSummery> arrayList = new ArrayList<UnsafeBhvTimeSummery>();
		if(PubUtil.isNotEmpty(list)){
			int step = 0;
//			list.
			step = createUnsafeTimeList(list, arrayList, step);
			if(step < list.size()){
				step = createUnsafeTimeList(list, arrayList, step);
			}else{
				for(int i=0;i<24;i++){
					UnsafeBhvTimeSummery unsafe = UnsafeBhvTimeSummery.class.newInstance();
					String hourStr = (i<10)? "0"+i : ""+i;
					unsafe.setHour(hourStr);
					unsafe.setBhvCount(new BigDecimal(0));
					arrayList.add(unsafe);
				}
			}
		}
		return arrayList;
	}


	@SuppressWarnings("rawtypes")
	private int createUnsafeTimeList(List list,
			ArrayList<UnsafeBhvTimeSummery> arrayList, int step)
			throws InstantiationException, IllegalAccessException {
		for(int i=0;i<24;i++){
			UnsafeBhvTimeSummery unsafe = UnsafeBhvTimeSummery.class.newInstance();
			String hourStr = (i<10)? "0"+i : ""+i;
			unsafe.setHour(hourStr);
			if(list.size() > step){
				Map unsafeMap = (Map)list.get(step);
				String hour = (String)unsafeMap.get("HOURS");
				if(hourStr.equals(hour)){
					unsafe.setBhvCount((BigDecimal)unsafeMap.get("BHV_COUNT"));
					step ++ ;
				}else{
					unsafe.setBhvCount(new BigDecimal(0));
				}
			}else{
				unsafe.setBhvCount(new BigDecimal(0));
			}
			arrayList.add(unsafe);
		}
		return step;
	}

}
